package com.util.library.msg;

import com.hethongviet.library.R;

import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class DialogMessageBack extends Dialog
{

	private TextView _textView = null;

	private ImageView _imageViewError = null;
	private ImageView _imageViewInfo = null;

	private Button _buttonNO = null;

	private Activity _activity = null;

	public DialogMessageBack(Activity activity, boolean isMessageError)
	{
		super(activity);
		_activity = activity;

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.dialog_message_confirm);

		_textView = (TextView) findViewById(R.id.textDialogMessage);

		_imageViewError = (ImageView) findViewById(R.id.image_message_error);
		_imageViewInfo = (ImageView) findViewById(R.id.image_message_info);
		if (isMessageError)
		{
			_imageViewError.setVisibility(View.VISIBLE);
			_imageViewInfo.setVisibility(View.GONE);
		}
		else
		{
			_imageViewError.setVisibility(View.GONE);
			_imageViewInfo.setVisibility(View.VISIBLE);
		}
		showYesButton(false);
		_buttonNO = (Button) findViewById(R.id.buttonDialogMessage);
	}

	private void showYesButton(boolean isShow)
	{
		LinearLayout layoutYes = (LinearLayout) findViewById(R.id.layout_button_dialog_message_yes);

		Button buttonYes = (Button) findViewById(R.id.buttonDialogMessageYES);

		if (isShow)
		{
			layoutYes.setVisibility(View.VISIBLE);
			buttonYes.setVisibility(View.VISIBLE);
		}
		else
		{
			layoutYes.setVisibility(View.GONE);
			buttonYes.setVisibility(View.GONE);
		}
	}

	public DialogMessageBack(Activity activity, boolean isMessageError,
			String text, final boolean isFinishActivity)
	{
		this(activity, isMessageError);

		prepareResource(text, isFinishActivity);
	}
	
	private void prepareResource(String text, final boolean isFinishActivity)
	{

		View.OnClickListener onClickListenerNO = new View.OnClickListener() {

			@Override
			public void onClick(View v)
			{
				hide();
				// dismiss();
				if (isFinishActivity)
				{
					_activity.finish();
				}
			}
		};

		_textView.setText(text);

		_buttonNO.setText(_activity.getString(R.string.back));
		_buttonNO.setOnClickListener(onClickListenerNO);
	}

	public DialogMessageBack(Activity activity, boolean isMessageError,
			int textViewId, final boolean isFinishActivity)
	{
		this(activity, isMessageError);

		prepareResource(activity.getString(textViewId), isFinishActivity);

	}

}
